package com.onec.service.api.utils;


import org.apache.commons.lang3.RandomStringUtils;

import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * string map 之间的一些转换：
 */
public class StringMapUtil {


    /**
     * 将key=value& .... 转为map:
     * URLDecoder 对特殊字段
     *
     * @param content
     * @return
     */
    public static Map<String, Object> strTransForMap(String content) {
        assert content != null;
        String[] str = content.split("&");
        Map<String, Object> resultMap = new HashMap<>();
        for (String s : str) {
            String[] ms = s.split("=");
            String value = "";
            if (ms.length == 2) {
                value = ms[1];
            }
            resultMap.put(ms[0], URLDecoder.decode(value, Charset.defaultCharset()));
        }
        return resultMap;
    }

    /**
     * 将key=value& .... 转为map:
     * URLDecoder 对特殊字段
     *
     * @param content
     * @return
     */
    public static String getStrByLength(String content, int length) {
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHssmm");
        StringBuilder stringBuilder = new StringBuilder(content);
        stringBuilder.append(dateTimeFormatter.format(localDateTime)).append(RandomStringUtils.random(length));
        return stringBuilder.toString();
    }

}
